package com.zjw.demo.vertx.starter.config;

import com.zjw.demo.vertx.starter.annotation.NonNull;
import com.zjw.demo.vertx.starter.utils.LogUtils;
import com.zjw.demo.vertx.starter.core.Reflector;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


/**
 * 参考 application.properties
 */
public final class ApplicationConfig {

  private static final String TAG = "ApplicationConfig";

  /**
   * 一个可拓展功能的目录，
   *
   * @see com.zjw.demo.vertx.starter.plugin.PluginDispatcher
   * @see com.zjw.demo.vertx.starter.plugin.IPlugin
   */
  public static final String PLUGIN_PATH = "com.zjw.demo.vertx.starter.plugin.impl";

  private static final Properties configProperties = new Properties();

  public static void initConfig() {
    LogUtils.debug(TAG, "start config");

    InputStream resourceAsStream = Reflector.sApplication.getClassLoader().getResourceAsStream("application.properties");
    try {
      configProperties.load(resourceAsStream);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    LogUtils.debug(TAG, "end config");
  }

  @NonNull
  public static String getString(String key) {
    return configProperties.getProperty(key);
  }

  @NonNull
  public static int getInt(String key) {
    return Integer.parseInt(configProperties.getProperty(key));
  }

  @NonNull
  public static boolean getBoolean(String key) {
    return Boolean.parseBoolean(configProperties.getProperty(key));
  }
}
